# 导入模块
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

# 准备数据
UserInfo = {
    'user': 'yong_Afei',
    'password': '******'
}


class Email:
    # 初始化
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get('https://mail.163.com/')

        # 切换到登录iframe
        iframe = self.driver.find_element(By.TAG_NAME, 'iframe')
        self.driver.switch_to.frame(iframe)

    # 登录
    def logon(self):
        # 定位账号输入框，输入数据
        self.driver.find_element(By.NAME, 'email').send_keys(UserInfo['user'])
        # 定位到密码输入框，输入数据
        self.driver.find_element(By.NAME, 'password').send_keys(UserInfo['password'])
        # 定位下次免登录按钮
        self.driver.find_element(By.NAME, 'un-login').click()
        # 定位到登录按钮，点击
        self.driver.find_element(By.ID, 'dologin').click()

    # 发电子邮件
    def send_emails(self):
        # 定位到写信，点击
        self.driver.find_element(By.XPATH, '//*[@id="_mail_component_149_149"]/span[2]').click()
        # 定位收件人输入框，输入数据
        self.driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('meiyong')
        # 定位主题输入框，输入数据
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[2]/div[1]/div/div/input').send_keys('zuoye')
        # 切换到正文iframe
        iframe = self.driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
        self.driver.switch_to.frame(iframe)
        # 定位正文输入框，输入数据
        self.driver.find_element(By.XPATH, '/html/body').send_keys('zuoye')
        # 切换到发邮件
        self.driver.switch_to.default_content()
        # 定位发邮件按钮,点击
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[2]/div[1]/section/footer/div[1]/span[2]').click()

    def quit(self):
        # 退出
        self.driver.quit()


if __name__ == '__main__':
    lo = Email()
    time.sleep(5)
    lo.logon()
    time.sleep(5)
    lo.send_emails()
    time.sleep(5)
    lo.quit()
